package cn.dnui_xcz826.dnuifood_16110100826;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.ArrayList;

import cn.dnui_xcz826.dnuifood_16110100826.fragments.Fragment1;
import cn.dnui_xcz826.dnuifood_16110100826.fragments.Fragment2;
import cn.dnui_xcz826.dnuifood_16110100826.fragments.Fragment3;
import cn.dnui_xcz826.dnuifood_16110100826.fragments.Fragment4;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ViewPager viewPager;
    private ImageButton ib1,ib2,ib3,ib4;
    private TextView tv1,tv2,tv3,tv4;
    private ArrayList<Fragment> fragmentList;
    private FragmentPagerAdapter fragmentPagerAdapter;
    private FragmentPagerAdapter fpadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        initFragmentList();
    }
    private void initView(){
        ib1=findViewById(R.id.ib1);
        ib2=findViewById(R.id.ib2);
        ib3=findViewById(R.id.ib3);
        ib4=findViewById(R.id.ib4);
        tv1=findViewById(R.id.tv1);
        tv2=findViewById(R.id.tv2);
        tv3=findViewById(R.id.tv3);
        tv4=findViewById(R.id.tv4);
        viewPager=findViewById(R.id.viewpager);
    }
    private void initEvent(){
        ib1.setOnClickListener(this);
        ib2.setOnClickListener(this);
        ib3.setOnClickListener(this);
        ib4.setOnClickListener(this);
    }
    private void initFragmentList() {
        // 将fragment加载到list中
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new Fragment1());
        fragmentList.add(new Fragment2());
        fragmentList.add(new Fragment3());
        fragmentList.add(new Fragment4());
        fpadapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }
            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        viewPager.setAdapter(fpadapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                viewPager.setCurrentItem(position);
                resetImage();
                setSelectStatus(position);
            }
            @Override
            public void onPageScrollStateChanged(int state) {}
        });
    }
    private void resetImage(){
        ib1.setImageResource(R.drawable.ic_shouye_former);
        ib2.setImageResource(R.drawable.ic_search_former);
        ib3.setImageResource(R.drawable.ic_collect_former);
        ib4.setImageResource(R.drawable.ic_wo_former);
    }
    private void setSelectStatus(int index) {
        switch (index){
            case 0:
                ib1.setImageResource(R.drawable.ic_shouye_latter_two);
                ib2.setImageResource(R.drawable.ic_search_former);
                ib3.setImageResource(R.drawable.ic_collect_former);
                ib4.setImageResource(R.drawable.ic_wo_former);
                break;
            case 1:
                ib1.setImageResource(R.drawable.ic_shouye_former);
                ib2.setImageResource(R.drawable.ic_search_latter);
                ib3.setImageResource(R.drawable.ic_collect_former);
                ib4.setImageResource(R.drawable.ic_wo_former);
                break;
            case 2:
                ib1.setImageResource(R.drawable.ic_shouye_former);
                ib2.setImageResource(R.drawable.ic_search_former);
                ib3.setImageResource(R.drawable.ic_collect_latter);
                ib4.setImageResource(R.drawable.ic_wo_former);
                break;
            case 3:
                ib1.setImageResource(R.drawable.ic_shouye_former);
                ib2.setImageResource(R.drawable.ic_search_former);
                ib3.setImageResource(R.drawable.ic_collect_former);
                ib4.setImageResource(R.drawable.ic_wo_latter);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        resetImage();
        switch (view.getId()){
            case R.id.ib1:
                viewPager.setCurrentItem(0);
                setSelectStatus(0);
                break;
            case R.id.ib2:
                viewPager.setCurrentItem(1);
                setSelectStatus(1);
                break;
            case R.id.ib3:
                viewPager.setCurrentItem(2);
                setSelectStatus(2);
                break;
            case R.id.ib4:
                viewPager.setCurrentItem(3);
                setSelectStatus(3);
                break;
        }
    }
}